﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using PloobsEngine.Utils;
using PloobsEngine.Modelo.Animation;
using PloobsEngine.Modelo;
using PloobsEngine.SceneControl;
using PloobsEngine.Light;


namespace PloobsEngine.Material
{
    public class AnimatedMaterial : Material
    {
        private IAnimatedController controller;

        /// <summary>
        /// Serialization
        /// </summary>
        public AnimatedMaterial()
        {

        }

        public override void Serialize(System.Xml.XmlTextWriter textWriter)
        {
            SerializerHelper.SerializeNewElement("AnimationController", textWriter);
            SerializerHelper.SerializeAttributeBaseType<string>(controller.GetType().AssemblyQualifiedName, "Type", textWriter);
            controller.Serialize(textWriter);
            SerializerHelper.SerializeEndElement(textWriter);

            SerializerHelper.SerializeNewElement("AnimationShader", textWriter);
            SerializerHelper.SerializeAttributeBaseType<string>(Shadder.GetType().AssemblyQualifiedName, "Type", textWriter);
            Shadder.Serialize(textWriter);
            SerializerHelper.SerializeEndElement(textWriter);
        }

        public override void DeSerialize(System.Xml.XmlNode node,EngineStuff engine, IModelo model)
        {

            XmlElement mod = node["AnimationController"];
            string modtype = SerializerHelper.DeserializeAttributeBaseType<string>("Type", mod);
            Type tt = Type.GetType(modtype);
            IAnimatedController controller = Activator.CreateInstance(tt) as IAnimatedController;
            controller.DeSerialize(mod,engine,model as IAnimatedModel);
            this.controller = controller;

            XmlElement ans = node["AnimationShader"];
            string mt = SerializerHelper.DeserializeAttributeBaseType<string>("Type", ans);
            Type at = Type.GetType(mt);
            IShader shader = Activator.CreateInstance(at) as IShader;
            shader.DeSerialize(ans, engine,controller);
            this.Shadder = shader;



        }

        public AnimatedMaterial(IAnimatedController controller , SimpleAnimationShader shader,MaterialType type) :
            base(shader,type)
        {
            this.controller = controller;
        }

        public AnimatedMaterial(IAnimatedController controller, SimpleAnimationShader shader) :
            base(shader,MaterialType.DEFERRED)
        {
            this.controller = controller;
        }

        public AnimatedMaterial(IAnimatedController controller, CustomAnimationShader shader) :
            base(shader, MaterialType.DEFERRED)
        {
            this.controller = controller;
        }

        public AnimatedMaterial(IAnimatedController controller, CustomAnimationShader shader, MaterialType type) :
            base(shader, type)
        {
            this.controller = controller;
        }

        public override void  Update(Microsoft.Xna.Framework.GameTime gametime, IObject obj, IList<ILight> lights)
        {
            controller.Update(gametime);
            base.Update(gametime, obj,lights);
            
        }

    }
}
